package cn.liuhuan1.blogsModel.service;

import cn.liuhuan1.api.MongoDbClent;
import cn.liuhuan1.blogsModel.dao.WebUserDao;
import cn.liuhuan1.webModelBean.api.MongoDbImp;
import cn.liuhuan1.webModelBean.api.WebUserImp;
import cn.liuhuan1.webModelBean.vo.ArticleCommentVo;
import cn.liuhuan1.webModelBean.vo.WebUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: liuhuan
 * @Date: 2021/4/20 - 04 - 20 - 19:15
 * @Description:
 * @version: 1.0
 */
@Service
public class MongoDbService implements MongoDbImp {

    @Autowired
    private MongoDbClent mongoDbClent;

    @Autowired
    private WebUserImp webUserService;

    @Autowired
    private ArticleService articleService;

    @Override
    public void insert(ArticleCommentVo articleCommentVo){
        if(articleCommentVo.getWebUserId() != null && articleCommentVo.getWebUserId().trim() != ""){
            WebUserVo webUserVo = webUserService.getById(articleCommentVo.getWebUserId());
            articleCommentVo.setUserName(webUserVo.getName());
            articleCommentVo.setUserHeadUrl(webUserVo.getHeadPortrait());
        }
        if(articleCommentVo.getArticleId() != null && articleCommentVo.getArticleId().trim() != ""){
            articleService.updateReplyNumber(articleCommentVo.getArticleId());
            }
        mongoDbClent.insert(articleCommentVo);
    }

    @Override
    public void insertAll(List<ArticleCommentVo> articleCommentVoList){
        for (ArticleCommentVo articleCommentVo : articleCommentVoList) {
            if(articleCommentVo.getWebUserId() != null && articleCommentVo.getWebUserId().trim() != ""){
                WebUserVo webUserVo = webUserService.getById(articleCommentVo.getWebUserId());
                articleCommentVo.setUserName(webUserVo.getName());
                articleCommentVo.setUserHeadUrl(webUserVo.getHeadPortrait());
            }
        }
        mongoDbClent.insertList(articleCommentVoList);
    }
    @Override
    @Transactional
    public List<ArticleCommentVo> findAll(String articleId){
        List<ArticleCommentVo> mongoDbImpAll = mongoDbClent.findAll(articleId);
        return mongoDbImpAll;
    }

    @Override
    public void likeNumber(String id,Integer likeNumber) {
        mongoDbClent.likeNumber(id,likeNumber);
    }
}
